package logic

import (
	"yundian/internel/app/dao"
	"yundian/internel/app/models/users"
)

type UserLogic struct {
}

func NewUserLogic() *UserLogic {
	return &UserLogic{}
}

// 递归查找 代理商下级
func (logic UserLogic) FindAgentChildrenList(pid int, userInfo *users.UserInfoWithAgent, isAdmin uint8) {
	lists := dao.NewUserDao().GetAgentUserListByParentId(pid, isAdmin).Unwrap().([]*users.UserInfoWithAgent)
	if len(lists) == 0 {
		return
	}
	for _, lst := range lists {
		userInfo := dao.NewUserDao().GetUserInfoById(lst.ParentId).Unwrap().(*users.UserModel)
		lst.ParentName = userInfo.UserName + "-" + userInfo.Name
		logic.FindAgentChildrenList(lst.ID, lst, isAdmin)
	}
	userInfo.Children = lists
}

// 递归查找 代理商下级
func (logic UserLogic) FindIsAdminChildrenList(pid int, userInfo *users.UserInfoWithAgent, isAdmin uint8) {
	lists := dao.NewUserDao().GetIsAdminUserListByParentId(pid, isAdmin).Unwrap().([]*users.UserInfoWithAgent)
	if len(lists) == 0 {
		return
	}
	for _, lst := range lists {
		userInfo := dao.NewUserDao().GetUserInfoById(lst.ParentId).Unwrap().(*users.UserModel)
		lst.ParentName = userInfo.UserName + "-" + userInfo.Name
		logic.FindAgentChildrenList(lst.ID, lst, isAdmin)
	}
	userInfo.Children = lists
}

// 递归查找 商户下级
func (logic UserLogic) FindPlaceChildrenList(pid int, userInfo *users.UserInfoWithPlace) {

	lists := dao.NewUserDao().GetPlaceUserListByParentId(pid).Unwrap().([]*users.UserInfoWithPlace)
	if len(lists) == 0 {
		return
	}
	for _, lst := range lists {
		userInfo := dao.NewUserDao().GetUserInfoById(lst.ParentId).Unwrap().(*users.UserModel)
		lst.ParentName = userInfo.UserName + "-" + userInfo.Name
		logic.FindPlaceChildrenList(lst.ID, lst)
	}
	userInfo.Children = lists
}

func (logic UserLogic) FindPlaceUserChildrenList(pid int, userInfo *users.UserInfoWithPlace) {

	lists := dao.NewUserDao().GetPlaceUserListByParentId(pid).Unwrap().([]*users.UserInfoWithPlace)
	if len(lists) == 0 {
		return
	}
	for _, lst := range lists {
		userInfo := dao.NewUserDao().GetUserInfoById(lst.ParentId).Unwrap().(*users.UserModel)
		lst.ParentName = userInfo.UserName + "-" + userInfo.Name
		logic.FindPlaceChildrenList(lst.ID, lst)
	}
	userInfo.Children = lists
}

// 递归查找 商户下级
//func (logic UserLogic) FindPlaceUserChildrenList(pid []int, userInfo *users.UserInfoWithPlace) {
//
//	//pidArr := []int{pid}
//	//tempUserIdArr := dao.NewUserDao().GetUserIdByPid(pidArr).Unwrap().([]int)
//	lists := dao.NewUserDao().GetPlaceUserListParentId(pid).Unwrap().([]*users.UserInfoWithPlace)
//	if len(lists) == 0 {
//		return
//	}
//	for _, lst := range lists {
//		userInfo := dao.NewUserDao().GetUserInfoById(lst.ParentId).Unwrap().(*users.UserModel)
//		lst.ParentName = userInfo.UserName + "-" + userInfo.Name
//		logic.FindPlaceChildrenList(lst.ID, lst)
//	}
//	userInfo.Children = lists
//}

func (logic UserLogic) GetProfit(preUserInfo *users.UserModel, profit int, userLists *[]*users.UserModel) []*users.UserModel {
	userInfo := dao.NewUserDao().GetUserInfoById(preUserInfo.ParentID).Unwrap().(*users.UserModel) // 父ID
	userInfo.RealProfit = userInfo.ProfitProportion - profit
	*userLists = append(*userLists, userInfo)
	if userInfo.ProfitProportion == 100 {
		return *userLists
	}
	if userInfo.ParentID != 0 {
		profit = userInfo.RealProfit + profit
		logic.GetProfit(userInfo, profit, userLists)
	}
	return *userLists
}

// 递归查找用户下级
func (logic UserLogic) FindUserChildrenList(pid int, userInfo *users.UserInfoWithAgent) {
	lists := dao.NewUserDao().GetUserListByParentId(pid).Unwrap().([]*users.UserInfoWithAgent)
	if len(lists) == 0 {
		return
	}
	for _, lst := range lists {
		logic.FindUserChildrenList(lst.ID, lst)
	}
	userInfo.Children = lists
}

// 递归查找用户下级
func (logic UserLogic) FindAllIsAdminUserChildrenList(pid int, userInfo *users.UserInfoWithAgent, isAdmin uint8) {
	lists := dao.NewUserDao().GetAllIsAdminUserListByParentId(pid, isAdmin).Unwrap().([]*users.UserInfoWithAgent)
	if len(lists) == 0 {
		return
	}
	for _, lst := range lists {
		logic.FindUserChildrenList(lst.ID, lst)
	}
	userInfo.Children = lists
}

// 递归查找用户下级
func (logic UserLogic) FindAllIsAdminUserChildrenListBatch(userList []*users.UserInfoWithAgent, isAdmin uint8, num int) {
	var ids []int
	var childrenIds []int
	for _, item := range userList {
		ids = append(ids, item.ID)
	}
	if len(ids) == 0 {
		return
	}

	lists := dao.NewUserDao().GetAllIsAdminUserListByParentIds(ids, isAdmin).Unwrap().([]*users.UserInfoWithAgent)
	if len(lists) == 0 {
		return
	}

	for _, lst := range lists {
		childrenIds = append(childrenIds, lst.ID)
		for _, item := range userList {
			if item.ID == lst.ParentId {
				item.Children = append(item.Children, lst)
				break
			}
		}
	}

	if len(childrenIds) > 0 && num < 100 {
		logic.FindAllIsAdminUserChildrenListBatch(lists, isAdmin, num+1)
	}
}

func (logic UserLogic) FindAllIsAdminUserListBatch(userList *[]*users.UserInfoWithAgent, isAdmin uint8, num int) {
	var ids []int
	var childrenIds []int
	for _, item := range *userList {
		ids = append(ids, item.ID)
	}
	if len(ids) == 0 {
		return
	}

	lists := dao.NewUserDao().GetAllIsAdminUserListByParentIds(ids, isAdmin).Unwrap().([]*users.UserInfoWithAgent)
	if len(lists) == 0 {
		return
	}

	//for _, lst := range lists {
	//	childrenIds = append(childrenIds, lst.ID)
	//	for _, item := range userList {
	//		if item.ID == lst.ParentId {
	//			item.Children = append(item.Children, lst)
	//			break
	//		}
	//	}
	//}

	for _, lst := range lists {
		*userList = append(*userList, lst)
		childrenIds = append(childrenIds, lst.ID)
		//for _, item := range userList {
		//	if item.ID == lst.ParentId {
		//		item.Children = append(item.Children, lst)
		//		break
		//	}
		//}
	}

	if len(childrenIds) > 0 && num < 100 {
		logic.FindAllIsAdminUserListBatch(&lists, isAdmin, num+1)
	}
}
